package com.ipzoe.web.controller.cms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.cms.domain.ArticleCategory;
import com.ipzoe.cms.service.IArticleCategoryService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 文章分类Controller
 * 
 * @author eric
 * @date 2020-09-01
 */
@RestController
@RequestMapping("/cms/category" )
public class ArticleCategoryController extends BaseController {

    @Autowired
    private IArticleCategoryService iArticleCategoryService;

    /**
     * 查询文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleCategory articleCategory)
    {
        startPage();
        LambdaQueryWrapper<ArticleCategory> lqw = new LambdaQueryWrapper<ArticleCategory>();
        List<ArticleCategory> list = iArticleCategoryService.list(lqw);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('cms:category:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        LambdaQueryWrapper<ArticleCategory> lqw = new LambdaQueryWrapper<ArticleCategory>();
        lqw.orderByAsc(ArticleCategory::getOrderNum);
        List<ArticleCategory> list = iArticleCategoryService.list(lqw);
        return AjaxResult.success(list);
    }



    /**
     * 导出文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:category:export')" )
    @Log(title = "文章分类" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(ArticleCategory articleCategory) {
        LambdaQueryWrapper<ArticleCategory> lqw = new LambdaQueryWrapper<ArticleCategory>(articleCategory);
        List<ArticleCategory> list = iArticleCategoryService.list(lqw);
        ExcelUtil<ArticleCategory> util = new ExcelUtil<ArticleCategory>(ArticleCategory. class);
        return util.exportExcel(list, "category" );
    }

    /**
     * 获取文章分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:category:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iArticleCategoryService.getById(id));
    }

    /**
     * 新增文章分类
     */
    @PreAuthorize("@ss.hasPermi('cms:category:add')" )
    @Log(title = "文章分类" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleCategory articleCategory) {
        articleCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iArticleCategoryService.save(articleCategory) ? 1 : 0);
    }

    /**
     * 修改文章分类
     */
    @PreAuthorize("@ss.hasPermi('cms:category:edit')" )
    @Log(title = "文章分类" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticleCategory articleCategory) {
        articleCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iArticleCategoryService.updateById(articleCategory) ? 1 : 0);
    }

    /**
     * 删除文章分类
     */
    @PreAuthorize("@ss.hasPermi('cms:category:remove')" )
    @Log(title = "文章分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iArticleCategoryService.removeByIds(Arrays.asList(ids), SecurityUtils.getUsername()) ? 1 : 0);
    }
}
